package com.leaf.drawshapeexample;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{
	
	private SurfaceHolder m_surfaceholder;
	private Thread m_mainthread;
	private boolean m_is_running;
	private Paint m_paint;
	private Canvas m_lockcanvas;
	private int m_screenwidth, m_screenheight;	
	private ArrayList<PointF> m_shape_vertices;
	private boolean m_is_drawncustomshape;
	private Path m_path_to_draw;
	private RectF m_oval_rect, m_arc_rect, m_round_rect;

	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);
		setFocusable(true);
		m_screenwidth = context.getResources().getDisplayMetrics().widthPixels;
		m_screenheight = context.getResources().getDisplayMetrics().heightPixels;
	}
	
	public void Initialize(){
		m_oval_rect = new RectF(m_screenwidth/4 - m_screenwidth/8, m_screenwidth/4 - m_screenwidth/8, m_screenwidth/4 + m_screenwidth/8, m_screenwidth/4 + m_screenwidth/8);
		m_arc_rect = new RectF(m_screenwidth/2 + m_screenwidth/4 - m_screenwidth/8, m_screenwidth/4 - m_screenwidth/8,  m_screenwidth/2 + m_screenwidth/4 + m_screenwidth/8, m_screenwidth/4 + m_screenwidth/8);
		m_round_rect = new RectF(m_screenwidth/4 - m_screenwidth/8, m_screenheight/2 - m_screenwidth/8, m_screenwidth/4 + m_screenwidth/8, m_screenheight/2 + m_screenwidth/8);
		m_is_drawncustomshape = false;
		m_path_to_draw = new Path();
		m_shape_vertices = new ArrayList<PointF>();
		m_paint = new Paint();
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
	}
	
	public void GameExit(){
		//TODO destroy all variable here
		
	}
	
	public void run(){
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}
	}
	
	public void GameUpdate(){
		
	}
	
	
	
	public void GameDraw(Canvas canvas){
		m_paint.setStyle(Paint.Style.STROKE);
		m_paint.setAntiAlias(true);
		m_paint.setStrokeWidth(0);		
		canvas.drawColor(Color.WHITE);
		m_paint.setColor(Color.BLACK);
		canvas.drawText("Vertives Count: " + m_shape_vertices.size(), 5, 15, m_paint);
		int i_debug_sring_pos_y = 15;
		for(int i = 0; i < m_shape_vertices.size(); i ++){
			canvas.drawText("Point: " + m_shape_vertices.get(i).x + ", " + m_shape_vertices.get(i).y, 5, i_debug_sring_pos_y += 15, m_paint);
		}		
		m_paint.setStrokeWidth(3);
		m_paint.setColor(Color.CYAN);
		canvas.drawArc(m_arc_rect, 0, 90, true, m_paint);
		m_paint.setColor(Color.MAGENTA);
		canvas.drawRoundRect(m_round_rect, 0, 0, m_paint);
		m_paint.setColor(Color.YELLOW);
		canvas.drawOval(m_oval_rect, m_paint);
		m_paint.setColor(Color.BLUE);
		canvas.drawCircle(m_screenwidth/2 + m_screenwidth/4 - m_screenwidth/10, m_screenheight/2 - m_screenwidth/10, m_screenwidth/10, m_paint);
		m_paint.setColor(Color.GREEN);
		if(!m_is_drawncustomshape){
			if(m_shape_vertices.size() > 1){
				for(int i = 1; i < m_shape_vertices.size(); i ++)
				canvas.drawLine(m_shape_vertices.get(i-1).x, m_shape_vertices.get(i-1).y, m_shape_vertices.get(i).x, m_shape_vertices.get(i).y, m_paint);
			}
		}else{
			m_paint.setStyle(Paint.Style.FILL);
			canvas.drawPath(m_path_to_draw, m_paint);
			canvas.drawTextOnPath("Hello", m_path_to_draw, 0, 0, m_paint);
		}
		

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		if(e.getActionMasked() == MotionEvent.ACTION_UP || e.getActionMasked() == MotionEvent.ACTION_POINTER_UP){
			m_shape_vertices.add(new PointF(e.getX(), e.getY()));
		}
		if(e.getActionMasked() == MotionEvent.ACTION_DOWN || e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){
			if(m_is_drawncustomshape)
				ResetCustomShape();
		}
		return true;
	}
	
	public void DrawCustomShape(){
		m_is_drawncustomshape = true;
		if(m_shape_vertices.size() > 2){
			m_path_to_draw.moveTo(m_shape_vertices.get(0).x, m_shape_vertices.get(0).y);
			for(int i = 1; i < m_shape_vertices.size(); i ++){
				m_path_to_draw.lineTo(m_shape_vertices.get(i).x, m_shape_vertices.get(i).y);
			}
			m_path_to_draw.lineTo(m_shape_vertices.get(0).x, m_shape_vertices.get(0).y);
		}
	}
	
	public void ResetCustomShape(){
		m_is_drawncustomshape = false;
		for(int i = m_shape_vertices.size() - 1; i >= 0; i --){
			m_shape_vertices.remove(i);
		}
		m_path_to_draw.reset();
	}
	
	public boolean IsDrawnCustomShape(){
		return m_is_drawncustomshape;
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
//		m_screenwidth = context.getResources().getDisplayMetrics().widthPixels;
//		m_screenheight = context.getResources().getDisplayMetrics().heightPixels;
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}
}
